import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, getConnection } from 'typeorm';
import { UserRoleRelation as Entity } from '@/entity/user/user_role_relation.entity';

@Injectable()
export class UserRoleDao {

    public connection = getConnection();
    constructor(@InjectRepository(Entity)
    private readonly repository: Repository<Entity>,
    ) { }

    /**
     * 查询所有
     */
    findAll(param: any): Promise<Entity[]> {
        return this.repository.find({ order: { id: "DESC" } });
    }

    /**
     * 查询一条
     */
    findOne(params: any): Promise<Entity> {
        return this.repository.findOne({user_id:params.id});
    }

    /**
     * 添加
     */
    add(params: any): Promise<Entity> {
        let entity = new Entity();
        Object.assign(entity,params);
        return this.repository.save(entity);
    }

    /**
     * 修改
     * @param params 
     */
    async update(params: any): Promise<Entity> {
        let entity = await this.repository.findOne(params.id);
        Object.assign(entity,params);
        return this.repository.save(entity);
    }

    /**
     * 删除
     * @param params 
     */
    async delete(params: any): Promise<Entity> {
        let entity = await this.repository.findOne(params.id);
        return this.repository.remove(entity);
    }
}
